define([ "jquery","comm"],  function($,comm) {
	function Glable_mask() {
		this._container = null;
		this._shade = null;
		this.sub= null;
	}
	Glable_mask.prototype.init = function() {
		/*if($("#dialog_shade").length>0)
		{
			this._container=$("#glable_mask");
			this._shade = $("#dialog_shade");
			return;
		}*/
		this._shade = $("<div id='dialog_shade'/>");
		this._shade.css({
			"position": "fixed",
			"top": "0",
			"left": "0",
			"bottom":"0",
			"z-index": "1000",
			"width": "100%",
			"height": $(window).height(),
			"opacity": "0.6",
			"backgroundColor": "#333"
		});
		this._container = $("<div id='glable_mask'/>");
		this._container.css("position","fixed");
		this._container.css("top","0");
		this._container.css("left","0");
		this._container.css("right","0");
		this._container.css("z-index","1000");
		this._container.css("width","100%");
		this._container.css("height","100%");
		this._container.css("opacity", "0");
		this._container.height($(window).height());
		var that = this;
		this._container.click(function(e){
			comm.stopEvent(e);
			
			if(that.clickFun != null){
				that.clickFun();
			}
			
		});
		$("body").append(this._shade);
		$("body").append(this._container);
		
		$(window).resize(function(){
			that._shade.height($(window).height());
			that._container.height($(window).height());
			that.update();
		});
		
		$(document).off("keydown");
		$(document).on("keydown", function (e) {
			comm.stopEvent(e);
			var oEvent = window.event ? e.keyCode : e.which;
			if (oEvent == 27) {
				comm.stopEvent(e);
				that.remove();
				
			}
		});
	}

	/**
	 * 添加加载视图
	 * @param element
	 * @param msg
	 */
	Glable_mask.prototype.addLoadingView = function(msg)
	{
		var that = this;
		var target = $(that.sub);
		msg = msg || "";
		this.loadingView = document.createElement("div");
		//this.loadingMask = document.createElement("div");
		var p = document.createElement("p");
		$(this.loadingView).append(p);
		target.find(".content")/*.append(this.loadingMask)*/.append(this.loadingView);
		var panel = target.find(".panel");
		panel.find(".content").addClass("loading");
		var paddingBt = parseInt(panel.css("paddingBottom"))||0;
		//console.log(paddingBt);
		var height = panel.outerHeight()+paddingBt-5;
		$(p).text(msg).css({height:height,lineHeight:height+"px"});
		//$(this.loadingMask).addClass("win-loading-mask").css({width:panel.outerWidth(),height:height}).show();
		$(this.loadingView).addClass("win-loading")/*.css({width:panel.outerWidth(),height:height})*/.show();

		that.update();
	}
	Glable_mask.prototype.removeLoadingView = function(){
		if($(this.loadingView).length>0)
		{
			$(this.sub).find(".content").removeClass("loading");
			$(this.loadingView).remove();
			//this.loadingMask.remove();
		}
	}
	Glable_mask.prototype.add = function(element,isHide,cancelCb) {
		this.init();
		this.sub = element;
		var that=this;
		this._container.append(element).find(".close").click(function(){
			that.remove();
			if($.isFunction(cancelCb))
			{
				cancelCb();
			}
		});
		var scrollTop = $(window).scrollTop();
		var browseHeight = $(window).height();
		var c_height = this.sub.find(".panel").height();
		var last_height = scrollTop+(browseHeight-c_height)/2;
		this.sub.css("margin-top",last_height);
		if(isHide){
			this._container.css("opacity", "0");
			return;
		}
		this._container.css("opacity", "1");
	}
	
	Glable_mask.prototype.update = function(animate) {
		var scrollTop = $(window).scrollTop();
		var browseHeight = $(window).height();
		var c_height = this.sub.find(".panel").height();
		var last_height = scrollTop+(browseHeight-c_height)/2;
		if(animate)
		{
			this.sub.animate({"margin-top":last_height},"fast");
		}
		else{
			this.sub.css("margin-top",last_height);
		}

		this._container.css("opacity", "1");
	}
	
	Glable_mask.prototype.remove = function() {
		if(this._container)
		{
			this._container.remove();
		}
		if(this._shade)
		{
			this._shade.remove();
		}
	}
	
	Glable_mask.prototype.click = function(clickFun) {
		this._container.off().on("click",function(e){
			stopEvent(e);
			clickFun();
		});
	}
	
	return Glable_mask;
});